<template>
  <el-form
    :model="form"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    size="small"
    class="form-data"
  >
    <el-form-item label="课程名称" :label-width="formLabelWidth+'px'" prop="title">
      <el-input v-model="form.title" placeholder="请输入课程名称" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="课程难易程度" :label-width="formLabelWidth+'px'"  prop="level">
      <el-select v-model="form.level" placeholder="请选择课程难易程度">
        <el-option label="初级" value="初级"></el-option>
        <el-option label="中级" value="中级"></el-option>
        <el-option label="高级" value="高级"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="报名人数" :label-width="formLabelWidth+'px'" prop="count">
      <el-input v-model="form.count" placeholder="请输入课程报名人数" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="上线时间" prop="date" :label-width="formLabelWidth+'px'">
      <el-date-picker type="date" placeholder="选择日期" v-model="form.date" value-format="yyyy-MM-dd"></el-date-picker>
    </el-form-item>
    <el-form-item label="技术栈" prop="type" :label-width="formLabelWidth+'px'">
      <el-radio-group v-model="form.type">
        <el-radio label="vue" name="type"></el-radio>
        <el-radio label="react" name="type"></el-radio>
        <el-radio label="angular" name="type"></el-radio>
        <el-radio label="node" name="type"></el-radio>
        <el-radio label="小程序" name="type"></el-radio>
        <el-radio label="Ts" name="type"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item  :label-width="formLabelWidth+'px'" prop="title">
      <el-button @click="submitForm('ruleForm')" type="primary">创建课程</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts">
import { Component, Vue, Provide } from "vue-property-decorator";
@Component({
  components: {}
})
export default class FormData extends Vue {
  @Provide() formLabelWidth: number = 120; //搜索框
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  @Provide() form: object = {
    title: "",
    type: "",
    level: "",
    count: "",
    date: ""
  };
  @Provide() rules: any = {
    title: [{ required: true, message: "请输入课程标题", trigger: "blur" }],
    level: [
      { required: true, message: "请选择课程难易程度", trigger: "change" }
    ],
    count: [{ required: true, message: "请选择课程报名人数", trigger: "blur" }],
    date: [
      {
        type: "string",
        required: true,
        message: "请选择日期",
        trigger: "change"
      }
    ],
    type: [
      {
        required: true,
        message: "请选择技术栈",
        trigger: "change"
      }
    ]
  };
  submitForm(formName: string) {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        (this as any).$axios
          .post("/api/profiles/add", this.form)
          .then((res: any) => {
            (this as any).$message({
              message: res.data.msg,
              type: "success"
            });
            this.resetForm(formName);
          })
          .catch((error: any) => {});
      } else {
        console.log("error submit!!");
        return false;
      }
    });
  }
  resetForm(formName: string) {
    (this as any).$refs[formName].resetFields();
  }
}
</script>
<style lang="scss" scoped>
.form-data {
  //设置圆角
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  //设置上下左右的宽度
  padding: 35px;
  background: #fff;
  border: 1px solid #dcdfe6;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);

  .el-input,
  .el-select {
    width: 200px !important;
  }
  .division {
    margin: 0 10px;
    color: #606266;
  }
}
</style>